//
//  M4LimitAlertView.swift
//  M4
//
//  Created by YuChuang on 2024/9/11.
//

import UIKit

class M4LimitAlertView: UIView {
    
    @IBOutlet weak var M4LimitNameView: UIView!
    
    @IBOutlet weak var M4LimitAlertCenterY: NSLayoutConstraint!
    
    @IBOutlet weak var M4LimitCloseButton: UIButton!
    
    @IBOutlet weak var M4DecriptionView: M4DecriptionStringView!
    var M4LimitCloseBlock: (() -> Void)?
    
    override func awakeFromNib() {
        self.frame = CGRect(x: 0, y: 0, width: M4ScreenWidth, height: M4ScreenHeight)
    }
    
    func M4LimitAlertViewUpdate(nameArray: [[String : String]]) {
        for i in 0..<nameArray.count {
            let item = M4LimitNameItem()
            M4LimitNameView.addSubview(item)
            item.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalToSuperview().offset((17+18)*i)
                make.height.equalTo(17)
                if i == nameArray.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            item.M4LimitIcon.image = UIImage(named: nameArray[i]["img"] ?? "")
            item.M4LimitName.text = nameArray[i]["name"]
        }
    }
    
    @IBAction func M4LimitToSettingAction(_ sender: UIButton) {
        UIApplication.shared.open(NSURL(string: UIApplication.openSettingsURLString)! as URL)
        M4AlertViewHidden()
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4LimitSetToHtmlAction(_ sender: UIButton) {
        M4AlertViewHidden()
        let page = M4HtmlWebViewController()
        page.M4WebTitle = "Cómo establecer permisos manualmente?"
        page.M4WebUrl = M4NetworkAPI.m4htmlPath.rawValue + "/hkgjkv"
        page.M4FormLimit = true
        page.M4LimitBlock = {
            self.M4AlertViewShow()
        }
        M4BaseViewController.M4CurrentVC()?.navigationController?.pushViewController(page, animated: true)
        sender.M4EventUnavailable = false
    }
    
    @IBAction func M4LimitAlertClose(_ sender: UIButton) {
        M4LimitCloseBlock?()
        M4AlertViewHidden()
        sender.M4EventUnavailable = false
    }
    

}

class M4LimitNameItem: UIView {
    
    lazy var M4LimitIcon: UIImageView = {
        let img = UIImageView()
        return img
    }()
    
    lazy var M4LimitName: UILabel = {
        let label = UILabel()
        label.textColor = COLOR_333333
        label.font = UIFont.systemFont(ofSize: 14, weight: .bold)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        self.addSubview(M4LimitIcon)
        self.addSubview(M4LimitName)
        M4LimitIcon.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        M4LimitName.snp.makeConstraints { make in
            make.left.equalTo(M4LimitIcon.snp.right).offset(6)
            make.right.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
